package com.charging.common.utils;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;


import com.charging.common.constant.Constants;
import com.charging.common.utils.uuid.UUID;
import com.google.zxing.EncodeHintType;

import net.glxn.qrgen.core.AbstractQRCode;
import net.glxn.qrgen.core.image.ImageType;
import net.glxn.qrgen.javase.QRCode;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;


@Component
public class ImageUtils {
    protected Logger logger = LoggerFactory.getLogger(getClass());

    public void getQRCode(String equipNo,Integer channelNo,String valuePath)  {
        String srcImgPath = "base.png";
        String qrImgPath = "logs/"+ UUID.fastUUID()+".png";
        ArrayList<ImageDTO> list = new ArrayList<>();
        if (channelNo == 0){
            list.add(createImageDTO("总",new Color(25,130,255,100),new Font("", Font.BOLD, 60), 252, 103));
        } else if(channelNo>9){
            list.add(createImageDTO(channelNo.toString(),new Color(25,130,255,100),new Font("", Font.BOLD, 60), 252, 103));
        }else {
            list.add(createImageDTO(channelNo.toString(),new Color(25,130,255,100),new Font("", Font.BOLD, 70), 272, 105));
        }
        list.add(createImageDTO(equipNo,new Color(0,0,0,100),new Font("", Font.PLAIN, 40), 208, 550));
        //操作图片:
        writeImage(srcImgPath, valuePath, list);
        if(channelNo == 0){
            String url = Constants.kxSystemInfo.getBaseUrl() + "#/gocharge?equipNo="+equipNo;
            createQRCode(qrImgPath,url);
        }else {
            String url = Constants.kxSystemInfo.getBaseUrl() + "#/gocharge?equipNo="+equipNo+"&channelNo="+channelNo;
            createQRCode(qrImgPath,url);
        }
        writeQR(valuePath,qrImgPath);
        new File(qrImgPath).delete();
    }

    public static void main(String [] args){

    }

    public void createQRCode(String qrPath,String url){
        AbstractQRCode qrCode = QRCode.from(url);
        // 设置字符集，支持中文
        qrCode.withCharset("utf-8");
        // 设置生成的二维码图片大小
        qrCode.withSize(350,350);

        qrCode.withHint(EncodeHintType.MARGIN,0);

        ByteArrayOutputStream out = qrCode.to(ImageType.PNG).stream();
        File file = new File(qrPath);
        FileOutputStream fout = null;
        try {
            fout = new FileOutputStream(file);
            fout.write(out.toByteArray());
            fout.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fout.close();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void writeQR(String basePath,String tarPath){
        FileOutputStream outImgStream = null;
        try {
            File srcImgFile = new File(basePath);//得到文件
            Image srcImg = ImageIO.read(srcImgFile);//文件转化为图
            int srcImgWidth = srcImg.getWidth(null);//获取图片的宽
            int srcImgHeight = srcImg.getHeight(null);//获取图片的高

            File tarImgFile = new File(tarPath);//得到文件
            Image tarImg = ImageIO.read(tarImgFile);//文件转化为图片
            int tarImgWidth = tarImg.getWidth(null);//获取图片的宽
            int tarImgHeight = tarImg.getHeight(null);//获取图片的高

            BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = bufImg.createGraphics();
            g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
            g.drawImage(tarImg, 120, 140, tarImgWidth, tarImgHeight, null);
            g.dispose();
            outImgStream = new FileOutputStream(basePath);
            ImageIO.write(bufImg, "png", outImgStream);
        } catch (Exception e) {
            logger.error("==== 系统异常::{} ====",e);
        }finally {
            try {
                if (null != outImgStream){
                    outImgStream.flush();
                    outImgStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 编辑图片,往指定位置添加文字
     * @param srcImgPath    :源图片路径
     * @param targetImgPath :保存图片路径
     * @param list          :文字集合
     */
    public void writeImage(String srcImgPath, String targetImgPath, List<ImageDTO> list) {
        FileOutputStream outImgStream = null;
        try {
            //读取原图片信息
            File srcImgFile = new File(srcImgPath);//得到文件
            Image srcImg = ImageIO.read(srcImgFile);//文件转化为图片
            int srcImgWidth = srcImg.getWidth(null);//获取图片的宽
            int srcImgHeight = srcImg.getHeight(null);//获取图片的高

            //添加文字:
            BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = bufImg.createGraphics();
            g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
            for (ImageDTO imgDTO : list) {
                g.setColor(imgDTO.getColor());                                  //根据图片的背景设置水印颜色
                g.setFont(imgDTO.getFont());                                    //设置字体
                g.drawString(imgDTO.getText(), imgDTO.getX(), imgDTO.getY());   //画出水印
            }
            g.dispose();

            // 输出图片
            outImgStream = new FileOutputStream(targetImgPath);
            ImageIO.write(bufImg, "jpg", outImgStream);
        } catch (Exception e) {
            logger.error("==== 系统异常::{} ====",e);
        }finally {
            try {
                if (null != outImgStream){
                    outImgStream.flush();
                    outImgStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 创建ImageDTO, 每一个对象,代表在该图片中要插入的一段文字内容:
     * @param text  : 文本内容;
     * @param color : 字体颜色(前三位)和透明度(第4位,值越小,越透明);
     * @param font  : 字体的样式和字体大小;
     * @param x     : 当前字体在该图片位置的横坐标;
     * @param y     : 当前字体在该图片位置的纵坐标;
     * @return
     */
    private ImageDTO createImageDTO(String text,Color color,Font font,int x,int y){
        ImageDTO imageDTO = new ImageDTO();
        imageDTO.setText(text);
        imageDTO.setColor(color);
        imageDTO.setFont(font);
        imageDTO.setX(x);
        imageDTO.setY(y);
        return imageDTO;
    }

    /**
     * 存放文本内容的类
     */
    static class ImageDTO {
        //文字内容
        private String text;
        //字体颜色和透明度
        private Color color;
        //字体和大小
        private Font font;
        //所在图片的x坐标
        private int x;
        //所在图片的y坐标
        private int y;

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }

        public Color getColor() {
            return color;
        }

        public void setColor(Color color) {
            this.color = color;
        }

        public Font getFont() {
            return font;
        }

        public void setFont(Font font) {
            this.font = font;
        }

        public int getX() {
            return x;
        }

        public void setX(int x) {
            this.x = x;
        }

        public int getY() {
            return y;
        }

        public void setY(int y) {
            this.y = y;
        }
    }
}
